import { h } from "koishi"
import { distance } from "fastest-levenshtein"

export const name = "-lnnbot-discord-innocent-souls-keyword-reactions"

export function apply(ctx) {
  ctx.platform("discord").guild("1232299938285617244").on("message", async session => {
    let text = h("", session.elements).toString(true)
    if (session.userId === session.selfId) text = text.replace(/^\*\*\u2068.*?\u2069:\*\* \u2068/, "")
    ctx.logger.debug(text)
    const match = (...args) => args.some(re => text.match(re))
    const react = emoji => session.bot.createReaction(session.channelId, session.messageId, emoji).then(() => ctx.sleep(2000), e => ctx.logger.warn(e))
    if (distance("world's shortest poem", text.toLowerCase()) < text.length / 2 - 3) await react("four_legged_m:1304424226345848852")
    if (match(/egg/i)) await react("🥚")
    if (match(/^\W*wake\W*$/i, /^\W*up\W*$/i, /\bwak(.{1,5}|.*\b)up/i)) await react("📢")
    if (match(/(8\W?|e[in]?n\W)yue/i)) await react("enn_yue:1304431894447783986")
    if (match(/[БƂ]/)) await react("shi_bo:1304428261148852337")
    if (match(/(^|\s)k(n\Wt|\W|$)/i)) await react("😇")
    if (match(/when\W*$/i)) await react("🔜")
    if (match(/(^|\s)wat($|\s)/i)) await react("❔")
    if (match(/(^|\s)br[ou]?($|\s)/i)) await react("😰")
    if (distance("divide by 7", text.toLowerCase()) < text.length / 2 - 3) await react("142857:1322616486048829483")
  })
}
